// Author: Domien Nowicki

#include "VersusGameWidget.h"
#include "HighscoreDialog.h"
#include "PaintPaletteWidget.h"
#include "ClockWidget.h"
#include <QHboxLayout>
#include <QInputDialog>
#include <sstream>
#include <iomanip>
using namespace std;

VersusGameWidget::VersusGameWidget(QWidget *parent, const Map& newMap, const Properties& properties, Highscore& highscore): QWidget(parent), VersusGame(newMap, properties, highscore)
{
	// Setup versus game ui
    gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(6);
    gridLayout->setMargin(9);

	topHboxLayout = new QHBoxLayout(this);

    textEdit = new QTextEdit(this);
    textEdit->setMaximumSize(QSize(16777215, 80));
    textEdit->setCursor(QCursor(static_cast<Qt::CursorShape>(0)));
    textEdit->setReadOnly(true);

	topHboxLayout->addWidget(textEdit);

	gridLayout->addLayout(topHboxLayout, 0, 0, 1, 1);

	hboxLayout = new QHBoxLayout(this);
    hboxLayout->setSpacing(6);
    hboxLayout->setMargin(0);

    gridLayout->addLayout(hboxLayout, 1, 0, 1, 1);

	labelStepCount = new QLabel(this);

    gridLayout->addWidget(labelStepCount, 2, 0, 1, 1);
}


VersusGameWidget::~VersusGameWidget()
{
}

// Get name to enter it to the highscores
bool VersusGameWidget::GetName(std::string *name)
{
    bool ok;

    QString text;
	
	do
	{
		text = QInputDialog::getText(this, tr("Enter your name"), tr("You have made it to the highscores!\nEnter your name:"), QLineEdit::Normal, "", &ok);

		// Make sure the user did not click ok with an empty name
	}
    while(ok && text.isEmpty());

	if (ok)
	{
		*name = text.toStdString();
	}

	return ok;
}

// Show the highscores
void VersusGameWidget::ShowHighscore(const Highscore& highscore)
{
	HighscoreDialog* dialog = new HighscoreDialog(this, highscore);

	dialog->show();
}

void VersusGameWidget::Started()
{
	emit started(this);
}

void VersusGameWidget::Stopped()
{
	emit stopped();
}


// Create new map widget
Map* VersusGameWidget::NewMap(const Map& map)
{
	MapWidget *mapWidget = new MapWidget(this, map);

	// Add widget
	hboxLayout->addWidget(mapWidget);

	return mapWidget;
}


// Create new clock widget
Clock* VersusGameWidget::NewClock(Clock::ClockType type)
{
	ClockWidget *clockWidget = new ClockWidget(this, type);

	// Connect time out signal
	QObject::connect(clockWidget, SIGNAL(Timedout()), this, SLOT(slotTimedout()));

	// Add widget
	topHboxLayout->addWidget(clockWidget);

	return clockWidget;
}

// Create new human player
Player* VersusGameWidget::NewHumanPlayer(Map* map)
{
	HumanPlayerWidget *playerWidget = new HumanPlayerWidget(this, this, map, GetProperties());

	// Add widget
	hboxLayout->insertWidget(0, playerWidget);

	return playerWidget;
}

// Output message to the screen
void VersusGameWidget::Output(std::string message)
{
	// Add the message to the message board
	textEdit->append(QString(message.c_str()));
}

// Show the amount of steps the player has done
void VersusGameWidget::ShowPlayerStepCount(int steps)
{
	ostringstream s;

	// Set label to the step count text
	s << "Player step count: " << steps;	
	labelStepCount->setText(s.str().c_str());
}

// Slot for when the clock timed out
void VersusGameWidget::slotTimedout()
{
	VersusGame::Timeout();
}

